package com.vodafone.nowplus.android.peoplemail.filter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.text.TextUtils;
import android.util.Log;

public class FilterManagerImpl360 implements FilterManager {

	
	private static final String LOG_TAG_MESSAGE_LIST = "messageList";

	private class FilterIconRule {
		private String filter;
		private String iconId;
		public FilterIconRule(String filter, String iconId) {
	        super();
	        this.filter = filter;
	        this.iconId = iconId;
        }
		public String getIconId() {
	        return iconId;
        }
		public boolean matches(String what) {
			return what.matches(filter);
		}
	}
	
	private FilterIconRule filterIconRule[] = { new FilterIconRule("(?i:.*@gmail.*)","filter_icon_google"),
			new FilterIconRule("(?i:.*@googlemail.*)","filter_icon_google"),
			new FilterIconRule("(?i:.*@yahoo.*)","filter_icon_custom"),
			new FilterIconRule("(?i:.*@hotmail.*)","filter_icon_winlive"),
			new FilterIconRule("(?i:.*@live.co.*)","filter_icon_winlive"),
			new FilterIconRule("(?i:.*@vodafone.*)","filter_icon_360"),
			new FilterIconRule("[^@]*","filter_icon_360"),
			};
	
	//private static final String ICON_FOLDER = "ic_menu_navigate";
	private List<MessageListFilter> preconfiguredFilters = new ArrayList<MessageListFilter>();
	private List<MessageListFilter> filters = new ArrayList<MessageListFilter>();
	private final static MessageListFilter FILTER_ALL = new MessageListFilter("(?i:.*Inbox)", FILTER_TYPE_INBOX, "all", "filter_icon_all");
	private final static MessageListFilter FILTER_360 = new MessageListFilter("(?i:Inbox)", FILTER_TYPE_INBOX, "360", "filter_icon_360");
	//private final static MessageListFilter FILTER_FOLDERLIST = new MessageListFilter("", FILTER_TYPE_FOLDERLIST, FILTER_TYPE_FOLDERLIST, ICON_FOLDER);
	
	
	/**
	 * @param name
	 * @param type
	 * @param filter
	 * @return
	 */
	private MessageListFilter createMessageListFilter(String name, String type, String filter) {
		String iconId = "filter_icon_custom";
		for(FilterIconRule fir : filterIconRule) {
			if(fir.matches(filter)) {
				iconId = fir.getIconId();
				break;
			}
		}
		return new MessageListFilter(filter, type, name, iconId);
	}
	
	private MessageListFilter createMessageListFilter(String type, String filter) {
		return createMessageListFilter(type, type, filter);
	}
	
	
	public FilterManagerImpl360() {
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@gmail.com/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@googlemail.com/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@yahoo.com/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@yahoo.de/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@gmx.de/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@live.com/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@hotmail.com/Inbox)"));
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@vodafone.*/Inbox)"));
		// TODO add preconfigured filters for other mail providers here
		
		// add filter preset for custom accounts
		preconfiguredFilters.add( createMessageListFilter(FILTER_TYPE_INBOX, "(?i:.*@.*/Inbox)"));
		
		clearFilters();
    }
	
	@Override
	public void clearFilters() {
		//Log.v("messageList", "clearing filters, adding default filter");
		filters.clear();
		filters.add(FILTER_ALL);
		filters.add(FILTER_360);
		//filters.add(FILTER_FOLDERLIST);
	}
	
	@Override
	public boolean isFilter(String type) {
		return (FILTER_TYPE_INBOX.equals(type) || FILTER_TYPE_CUSTOM.equals(type) || FILTER_TYPE_FOLDERLIST.equals(type));
	}

	@Override
	public List<MessageListFilter> getFilters() {
		List<MessageListFilter> res = new ArrayList<MessageListFilter>();
		for(MessageListFilter mlf : filters) {
			if(isFilter(mlf.getType())) {
				res.add(mlf);
			}
		}
		Collections.sort(res);
		return res;
	}


	@Override
	public List<MessageListFilter> getFolders() {
		List<MessageListFilter> res = new ArrayList<MessageListFilter>();
		for(MessageListFilter mlf : filters) {
			if(FILTER_TYPE_FOLDER.equals(mlf.getType())) {
				res.add(mlf);
			}
		}
		Collections.sort(res);
		return res;
	}
	
	@Override
	public boolean matchesFilter(MessageListFilter filter, String folderName) {
		if(filter==null) {
			// unknown filter
			return false;
		}
		return filter.matches(folderName);
	}

	@Override
	public boolean matchesFilter(String filterName, String folderName) {
		return matchesFilter(getFilterByName(filterName), folderName);
	}

	@Override
	public void notifyOfExistingFolder(String folderName) {
		Log.v(LOG_TAG_MESSAGE_LIST, "FilterManager notified of folderName " + folderName);
		// is there a filter for this (except all)
		boolean filterExists=false;
		for(MessageListFilter f : getFilters()) {
			if(folderNameToFilterName(folderName, f).equals(f.getName())) {
				// we have this already, so do nothing
				Log.v(LOG_TAG_MESSAGE_LIST,"we have this already: " + f);
				filterExists=true;
				break;
			}
		}		
		if(!filterExists) {
			Log.v(LOG_TAG_MESSAGE_LIST,"not yet in filter list");
			// look for preconfigured filters which match folderName and add them to actual filter list once
			for(MessageListFilter f : preconfiguredFilters) {
				if(f.matches(folderName)) {
					// create a new filter based on this folderName if not yet existing with the same name
					String filterName = folderNameToFilterName(folderName, f);
					MessageListFilter newFilter = new MessageListFilter(filterName.trim(), FILTER_TYPE_CUSTOM, f.getIconId());
					// set name derived from this folderName (use email address)
					newFilter.setName(filterName.trim());
	
					Log.v(LOG_TAG_MESSAGE_LIST, "adding new filter " + newFilter + " fitting for preconfiguredFilter " + f);
					// construct a regex matching for this particular folder
					String matchRegex = "(?i:"+folderName+")";
					newFilter.setMatchRegex(matchRegex );
					newFilter.setFolder(folderName);
					filters.add(newFilter);
					// only add the first match.
					break;
				}
			}
		}
		// is there a folder filter for this?
		boolean folderExists=false;
		for(MessageListFilter f : getFolders()) {
			if(folderName.equals(f.getFolder())) {
				// we have this already, so do nothing
				Log.v(LOG_TAG_MESSAGE_LIST,"we have this already: " + f);
				folderExists=true;
				break;
			}
		}		
		if(!folderExists) {
			Log.v(LOG_TAG_MESSAGE_LIST,"not yet in folder list");
			MessageListFilter newFilter = createMessageListFilter(FILTER_TYPE_FOLDER, folderName);
			newFilter.setFolder(folderName);
			newFilter.setName(folderName);
			filters.add(newFilter);
		}
	}

	private String folderNameToFilterName(String folderName, MessageListFilter f) {
		if(folderName.length()==0) {
			return "";
		}
	    String filterName = TextUtils.split(folderName, "/")[0];
	    // is this not an aggregated account's inbox?
	    if(!filterName.contains("@")) {
	    	// no it isn't, so use the preconfigured filter's name.
	    	filterName = f.getName();
	    }
	    return filterName;
    }


	@Override
    public MessageListFilter getFilterForFolderName(String folderName) {
		for(MessageListFilter f : filters) {
			if(f.matches(folderName) && !FILTER_ALL.equals(f)) {
				return f;
			}
		}
		return null;
    }

	@Override
    public MessageListFilter getFilterByName(String messageFilterName) {
		for(MessageListFilter f : filters) {
			if(f.getName().equals(messageFilterName) && !f.getType().equals(FILTER_TYPE_FOLDER)) {
				return f;
			}
		}
	    return null;
    }
	
	@Override
    public MessageListFilter getFolderByName(String messageFilterName) {
		for(MessageListFilter f : filters) {
			if(f.getName().equals(messageFilterName) && f.getType().equals(FILTER_TYPE_FOLDER)) {
				return f;
			}
		}
	    return null;
    }


}
